package Bunnies::QuoteCIBC;
use strict;
use warnings;
use LWP;
use LWP::ConnCache;

#https://www.onlinebrokerage.cibc.com/Customer/en/IEQuickQuote?symbol=td%20&exchange=T&type=DT&lang=en&R=0.9343974178191274
#https://www.onlinebrokerage.cibc.com/Customer/en/IEQuickQuote?symbol=td%20&exchange=US&type=DT&lang=en&R=0.4688103587832302
use constant {
    CIBC_URL => "https://www.onlinebrokerage.cibc.com/Customer/en/IEQuickQuote?symbol=##SYMBOL##%20&exchange=##EXCHANGE##&type=DT&lang=en&R=0.8921672464348376",
};

sub _init_ua
{
    my $max_conn = shift || 10;

    my $ua = LWP::UserAgent->new();
    $ua->env_proxy;
    my $conn_cache = LWP::ConnCache->new();
    $conn_cache->total_capacity([$max_conn]) ;
    $ua->conn_cache($conn_cache);
    $ua->agent('Mozilla/6.0');
    return $ua;
}

sub new {
    my ($CLASS, %options) = @_;
    my $self = { %options };

    $self->{ua} ||= (($options{max_conn}) and ($options{max_conn} > 0)) ?
                    _init_ua($options{max_conn}) : _init_ua();

    $self->{url} ||= CIBC_URL;

    bless($self,$CLASS);
    return $self;
}


sub parse {
    my ($self, $html) = @_;

=cut
    my $quote = { 
        last => 0,
        ask => 0,
        bid => 0,
        voulume => 0,
        net => 0,
    }
=cut

    my @lines = split("\n", $html);
    my $quote = undef;

    my $count = 0;
    foreach my $line (@lines) {
        if( $line !~ m/^<tr align="center" valign="center">/) {
            $count ++;
            next;
        }

        $count += 3;
        $quote->{last} = $lines[$count];
        $quote->{last} =~ s/\s+<td nowrap><p>([\d\.]+)<\/td>/$1/;

        $count ++;
        $quote->{net} = $lines[$count];
        $quote->{net} =~ s/\s+<td nowrap><p><img src=(.*)>([\d\.]+)<\/td>/$2/;
        $quote->{net} = -$quote->{net} if($1 eq '"images/dn_arrow.gif"');

        $count ++;
        $quote->{bid} = $lines[$count];
        $quote->{bid} =~ s/\s+<td nowrap><p>([\d\.]+)<\/td>/$1/;

        $count ++;
        $quote->{ask} = $lines[$count];
        $quote->{ask} =~ s/\s+<td nowrap><p>([\d\.]+)<\/td>/$1/;

        $count ++;
        $quote->{volume} = $lines[$count];
        $quote->{volume} =~ s/\s+<td nowrap><p>([\d,]+)<\/td>/$1/;
        $quote->{volume} =~ tr/,//d;

    }

    return $quote;
}

sub fetch {
    my ($self, $exchange, $symbol) = @_;

    #try to be flexible here
    if($exchange =~ m/T/i or
        $exchange =~ m/TSE/i or
        $exchange =~ m/CANADA/i or
        $exchange =~ m/CA/i or
        $exchange =~ m/Canadian/i) {
        $exchange = 'T';
    } elsif ($exchange =~ m/US/i or
        $exchange =~ m/USA/i or
        $exchange =~ m/America/i or
        $exchange =~ m/American/i) {
        $exchange = 'US';
    } else {
        warn "Unknown exchange: $exchange\n"; 
        return undef;
    }

    my $url = $self->{url}; 
    $url =~ s/(.*)##SYMBOL##(.*)##EXCHANGE##(.*)/$1$symbol$2$exchange$3/; 

    my $response = $self->{ua}->get($url);

    my $quote = undef;
    if ($response->is_success) {
        $quote = $self->parse($response->decoded_content);
    } else {
        warn "Failed to read $exchange:$symbol quote: $response->status_line\n";
    }

    return $quote;
}

1;
=cut
use Data::Dumper;

my $html = `cat ../data/tmp/cibc.result`;
my $result = parse_cibc($html);

print Dumper($result);
=cut
